package com.efast.cafe.portal.controller.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.PackagePublishIndentifierBean;
import com.efast.cafe.portal.bean.api.PackagePublishServiceBean;
import com.efast.cafe.portal.bean.api.PortalApiObjectDto;
import com.efast.cafe.portal.bean.api.PortalApiServiceBean;
import com.efast.cafe.portal.bean.api.PortalApiServiceQueryBean;
import com.efast.cafe.portal.bean.api.PortalApiServiceUpgradeBean;
import com.efast.cafe.portal.entity.api.PortalApiInputParam;
import com.efast.cafe.portal.service.api.IPortalApiServiceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;

/**
 * <p>
 * Controller
 * </p>
 * <p>
 * Table: portal_api_service - 服务定义
 * </p>
 *
 * @since 2017-08-10 02:38:48
 */
@Controller
@RequestMapping("/frame_center/api")
public class PortalApiServiceController extends AbstractController {

	@Autowired
	private IPortalApiServiceService portalApiServiceService;

	

	/**
	 * 新增服务
	 * 
	 * @param portalApiServiceBean
	 * @param request
	 * @return
	 * @Author Administrator
	 * @Date 2017年8月11日
	 *
	 */
	@RequestMapping("/service/add")
	public @ResponseBody ResultBean insertApiService(@RequestBody PortalApiServiceBean portalApiServiceBean,
			HttpServletRequest request) {
		// RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			//PortalApiServiceBean portalApiServiceBean =new PortalApiServiceBean(); 
			result.setSuccess(true);
			portalApiServiceService.insertApiService(portalApiServiceBean);
			result.setObject(portalApiServiceBean.getId().toString());
			// result.setMessage(context.getMessage(ADD_SUCCESS_TIP));
			result.setMessage(ADD_SUCCESS_TIP);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			}
		}
		return result;
	}

	/**
	 * 修改服务
	 * 
	 * @param portalApiServiceBean
	 * @param request
	 * @return
	 * @Author Administrator
	 * @Date 2017年8月11日
	 *
	 */
	@RequestMapping("/service/edit")
	public @ResponseBody ResultBean updateApiService(@RequestBody PortalApiServiceBean portalApiServiceBean,
			HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			portalApiServiceBean.setId(portalApiServiceBean.getServiceId());
			portalApiServiceService.updateApiService(portalApiServiceBean);
			result.setObject(portalApiServiceBean.getId().toString());
			// result.setMessage(context.getMessage(ADD_SUCCESS_TIP));
			result.setMessage(SUCCESS);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				result.setMessage(FAILURE);
			}
		}
		return result;
	}

	/**
	 * 删除一个服务
	 * 
	 * @param id
	 * @param request
	 * @return
	 * @Author Administrator
	 * @Date 2017年8月11日
	 *
	 */
	@RequestMapping("/service/delete")
	public @ResponseBody ResultBean deleteApiService(@RequestBody Map<String, Integer> map,
			HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			Integer id = map.get("id");
			result.setSuccess(true);
			portalApiServiceService.deleteApiService(id);
			// result.setMessage(context.getMessage(ADD_SUCCESS_TIP));
			result.setMessage(DEL_SUCCESS_TIP);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			}
		}
		return result;
	}

	/**
	 * 查询service详情
	 * 
	 * @param id
	 * @param request
	 * @return PortalApiServiceBean
	 * @Author Administrator
	 * @Date 2017年8月12日
	 */
	@RequestMapping("/service/detail")
	public @ResponseBody PortalApiServiceBean queryApiServiceDetail(@RequestBody Map<String, Integer> map,
			HttpServletRequest request) {
		Integer id = map.get("id");
		PortalApiServiceBean bean = portalApiServiceService.QueryPortalApiServiceDetailById(id);
		return bean;
	}

	/**
	 * 发布服务
	 * 
	 * @param packagePublishServiceBean
	 * @param packagePublishIndentifierBean
	 * @param request
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年8月15日
	 */
	@RequestMapping("service/publish")
	public @ResponseBody ResultBean publishApiService(@RequestBody PackagePublishServiceBean packagePublishServiceBean,
			PackagePublishIndentifierBean packagePublishIndentifierBean, HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			portalApiServiceService.publishPortalApiServiceList(packagePublishServiceBean,
					packagePublishIndentifierBean);
			result.setMessage(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(FAILURE);
		}
		return result;
	}

	/**
	 * 服务升版
	 * 
	 * @param request
	 * @param portalApiServiceUpgradeBean
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年8月19日
	 */
	@RequestMapping("service/upgrade")
	public @ResponseBody ResultBean upgradeApiService(HttpServletRequest request,
			@RequestBody PortalApiServiceUpgradeBean portalApiServiceUpgradeBean) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			portalApiServiceService.upgradePortalApiService(portalApiServiceUpgradeBean);
			result.setMessage(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(FAILURE);
		}
		return result;
	}

	/**
	 * 查询服务列表
	 * 
	 * @param request
	 * @param portalApiServiceUpgradeBean
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年8月19日
	 */
	@RequestMapping("service/list")
	public @ResponseBody DataTableVo<PortalApiObjectDto> queryApiServiceList(HttpServletRequest request,
			@RequestBody PortalApiServiceQueryBean queryBean) {
		queryBean = portalApiServiceService.queryPortalApiServiceList(queryBean);
		return outPageJSONArray_bootstrapDatatable(queryBean);
	}

	/**
	 * 查询系统的所有版本
	 * 
	 * @param request
	 * @param queryBean
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年8月22日
	 */
	@RequestMapping("service/versions")
	public @ResponseBody ResultBean queryApiServiceVersions(HttpServletRequest request,
			@RequestBody PortalApiServiceQueryBean queryBean) {
		// RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			queryBean = portalApiServiceService.queryPortalApiServiceVersions(queryBean);
			result.setMessage(SUCCESS);
			result.setObject(queryBean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(FAILURE);
		}
		return result;
	}
	
	@RequestMapping("service/queryServiceByUri")
	public @ResponseBody ResultBean queryServiceByUri(HttpServletRequest request,
			@RequestBody Map<String,String> map) {
		// RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		String serviceUri = "";
		String indentifier = "";
		try {
			result.setSuccess(true);
			if(map.containsKey("uri")){
				serviceUri = map.get("uri");
			}
			if(map.containsKey("indentifier")){
				indentifier = map.get("indentifier");
			}
			result.setObject(portalApiServiceService.queryServiceByUri(serviceUri,indentifier));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(FAILURE);
		}
		return result;
	}
	
}